package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.util.ModUtils.*;
import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.*;

@Mod.EventBusSubscriber
public class BloodShieldEvent {
    @SubscribeEvent
    public static void onLivingEntityBeingHurt(LivingHurtEvent livingHurtEvent){
        LivingEntity livingEntity = livingHurtEvent.getEntityLiving();
        MobEffectInstance effect = livingEntity.getEffect(ModMobEffects.BLOOD_SHIELD.get());

        if (Objects.nonNull(effect)){
            int amplifier = effect.getAmplifier();

            if (getLivingEntityHealthPercent(livingEntity) >= Math.max(95 - amplifier, 35)){
                getLogger().info(hyphenName("Blood Shield", 15));
                getLogger().info("玩家血量百分比为：" + getLivingEntityHealthPercent(livingEntity));

                float multiplyPercent = 1 - Math.min(0.35F + 0.01F * amplifier, 0.95F);

                livingHurtEvent.setAmount(livingHurtEvent.getAmount() * multiplyPercent);

                getLogger().info("减伤百分之：" + multiplyPercent);
            }
        }
    }
}
